import bus from '../../bus.js'
export default {
  name: 'v-img',
  // 获取两个参数，1为当前图片所属类型
  props: {
    imgType: String,
    imgTips: String,
    img: String
  },
  data() {
    return {
      upToken: null,
      form:{  
        fileList: [],
      }
    }
  },
  methods: {
    // 点击图片删除，去除下面的文档显示和图片
    handleRemove(file, fileList) {
      this.form.fileList.splice(0, 1);
      if (this.imgType === 'banner') {
        bus.$emit('changeBannerImage', '')
      }
      if (this.imgType === 'dynamic') {
        bus.$emit('changeDynamicImage', '')
      }
      if (this.imgType === 'service') {
        bus.$emit('changeServiceImage', '')
      }
      if (this.imgType === 'schoolcontract') {
        bus.$emit('changeSchoolContract', '')
      }
      if (this.imgType === 'schoolcover') {
        bus.$emit('changeSchoolCover', '')
      }
    },
    // 上传成功回调函数
    handleSuccess (response, file, fileList) {
        let addr = 'img.lexikj.com/';
        this.form.fileList.push({ name: file.name, url:addr + response.key});
        // 成功后将获取的图片地址通过广播发送到form表单
        // 如果是banner图
        if (this.imgType === 'banner') {
          bus.$emit('changeBannerImage', this.form.fileList[0].url);
        }
        if (this.imgType === 'dynamic') {
          bus.$emit('changeDynamicImage', this.form.fileList[0].url);
        }
        if (this.imgType === 'service') {
          bus.$emit('changeServiceImage', this.form.fileList[0].url)
        }
        if (this.imgType === 'schoolcontract') {
          bus.$emit('changeSchoolContract', this.form.fileList[0].url)
        }
        if (this.imgType === 'schoolcover') {
          bus.$emit('changeSchoolCover', this.form.fileList[0].url)
        }
    },
    // 上传失败回调函数
    handleError(err, file, fileList) {
        console.log(err)
    },
    // 获取token函数
    getToken(){
      APP.ajax('token', {}, res => {
        this.upToken = { 'token':res.data.data.token};
      })
    }
  },
  mounted () {
    // 获取token
    this.getToken();
  }  
}